package com.example.springboot.service.impl;

import com.example.springboot.dao.RepairDao;
import com.example.springboot.entity.Repair;
import com.example.springboot.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class RepairServiceImpl implements RepairService {
    @Autowired
    private RepairDao repairDao;

    @Override
    public List<Repair> NoDelivery() {
        return repairDao.NoDelivery();
    }

    @Override
    public List<Repair> FinishDelivery() {
        return repairDao.FinishDelivery();
    }

    @Transactional
    @Override
    public Repair queryRepairByUId(int userId) {
        if(userId > 0)
            return repairDao.queryRepairByUId(userId);
        else
            throw new RuntimeException("用户id不合法");
    }

    @Transactional
    @Override
    public Repair queryRepairByRId(int repairId) {
        if(repairId > 0)
            return repairDao.queryRepairByRId(repairId);
        else
            throw new RuntimeException("用户id不合法");
    }

    @Transactional
    @Override
    public boolean insertRepair(Repair RepairDate) {
        if (RepairDate.getRepairId() > 0 && RepairDate.getUserId()>0 && RepairDate.getRepairRate() >0 &&
                RepairDate.getRepairLocation() != null){
            try{
                if(repairDao.insertRepair(RepairDate))
                    return true;
                else
                    throw new RuntimeException("插入数据失败");
            }catch (Exception e){
                throw new RuntimeException("数据插入失败"+e.toString());
            }
        }
        else
            throw new RuntimeException("用户id,维修id，维修进度，维修地点不能为空");
    }

    @Transactional
    @Override
    public boolean updateRepair(Repair RepairDate) {
        if (RepairDate.getRepairId() > 0){
            try{
                if(repairDao.updateRepair(RepairDate))
                    return true;
                else
                    throw new RuntimeException("更新数据失败");
            }catch (Exception e){
                throw new RuntimeException("数据更新失败"+e.toString());
            }
        }
        else
            throw new RuntimeException("维修id不能为空");
        }

    @Transactional
    @Override
    public boolean delRepairByRid(int repairId) {
        if(repairId > 0){
            try{
                if(repairDao.delRepairByRid(repairId)){
                    return true;
                }else{
                    throw new RuntimeException("删除失败");
                }
            }catch (Exception e){
                throw new RuntimeException("删除失败"+e.toString());
            }
        }else{
            throw  new RuntimeException("维修id不合法");
        }
    }

    @Override
    public boolean delRepairByUid(int userId) {
        if(userId > 0){
            try{
                if(repairDao.delRepairByUid(userId)){
                    return true;
                }else{
                    throw new RuntimeException("删除失败");
                }
            }catch (Exception e){
                throw new RuntimeException("删除失败"+e.toString());
            }
        }else{
            throw  new RuntimeException("用户id不合法");
        }
    }
}
